package com.juzhencms.apps.busi.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.support.hsf.HSFJSONUtils;
import com.juzhencms.apps.base.common.config.Config;
import com.juzhencms.apps.base.common.database.DBTable;

import lombok.extern.slf4j.Slf4j;

/**
 * 定时任务，默认不开启，如需开启需要在启动类开启@EnableScheduling注解
 * @author xuexinfeng
 *
 */
@Service
@Slf4j
public class TimerService extends CommonService {
	
	/**
	 * 定期清理日志数据，目前是每10天执行一次，请根据实际情况修改
	 */
	//@Scheduled(cron = "0 0 0 1,10,20 * *")
    public void cleanlog(){
        //判断只在生产环境及测试环境执行
		if(Config.getValue("spring.profiles.active").equals("beta") || Config.getValue("spring.profiles.active").equals("prod")) {
			int limitnum=200000;
	        log.info("cleanlog,limit "+limitnum+" logs");
	        int lognum=this.getJdbcDao().querySqlForInt("select count(id) from "+DBTable.log);
	        if(lognum>limitnum) {
	        	int maxId=this.getJdbcDao().querySqlForInt("select id from "+DBTable.log+" order by id desc limit 0,1");
	        	int minId=maxId-limitnum;
	        	this.getJdbcDao().updateSql("delete from "+DBTable.log+" where id<=?", new Object[] {minId});
	        	log.info("delete "+(lognum-limitnum)+" logs");
	        }
		}
    }

	

}
